package com.bamboo.eduservice.controller;

import com.bamboo.commonutils.ResponseEntity;
import com.bamboo.eduservice.entity.EduTeacher;
import com.bamboo.eduservice.entity.vo.TeacherQuery;
import com.bamboo.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 讲师处理器
 *
 * @author jack.wu
 * @since 2020-06-30
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Resource
    private EduTeacherService teacherService;

    @ApiOperation(value = "无条件查询所有讲师")
    @GetMapping("findAll")
    public ResponseEntity findAll() {
        List<EduTeacher> teacherList = teacherService.list(null);
        return ResponseEntity.ok().data("list", teacherList);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("delete/{id}")
    public ResponseEntity removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                                        @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return ResponseEntity.ok();
        } else {
            return ResponseEntity.error();
        }
    }

    @ApiOperation(value = "带条件分页查询讲师")
    @PostMapping("list/{current}/{limit}")
    public ResponseEntity pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                               @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> records = teacherService.pageTeacherCondition(current, limit, teacherQuery);
        return ResponseEntity.ok().data("total", records.getTotal()).data("rows", records.getRecords());
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("add")
    public ResponseEntity addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return ResponseEntity.ok();
        } else {
            return ResponseEntity.error();
        }
    }

    @ApiOperation(value = "根据讲师id查询讲师")
    @GetMapping("info/{id}")
    public ResponseEntity getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return ResponseEntity.ok().data("teacher", eduTeacher);
    }

    @ApiOperation(value = "修改讲师")
    @PutMapping("update")
    public ResponseEntity updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag) {
            return ResponseEntity.ok();
        } else {
            return ResponseEntity.error();
        }
    }
}

